Ajax.CachedAutocompleter = Class.create(Ajax.Autocompleter, {
	
	cache : (decodeURIComponent(getCookie('laksmi.cache')).evalJSON() ? decodeURIComponent(getCookie('laksmi.cache')).evalJSON() : {}),

	offset : 0,		//cakephp model->find metódusának paramétere
	pagesize : 5,
	saveCacheToServerAt : 15,

	initialize : function($super, element, update, url, options){		//a new CachedAutocompleter hívás pillanatában fut le
		this.cache[options.cacheId] = this.cache[options.cacheId] ? this.cache[options.cacheId] : {};
		if(this.cache[options.cacheId]['lastToken'] === undefined)
			this.cache[options.cacheId]['lastToken'] = null;
		if(this.cache[options.cacheId]['lastUrl'] === undefined)
			this.cache[options.cacheId]['lastUrl'] = null;
		if(this.cache[options.cacheId]['count'] === undefined)
			this.cache[options.cacheId]['count'] = 0;
		$super(element, update, url, options);
	},

	getUpdatedChoices : function(){		//ajax hívás
		var token = this.getToken();
		if(token != this.cache[this.options.cacheId]['lastToken'] || this.url != this.cache[this.options.cacheId]['lastUrl']){
			this.offset = 0;
			this.cache[this.options.cacheId]['lastToken'] = token;
			this.cache[this.options.cacheId]['lastUrl'] = this.url;
		}
		if(this.cache[this.options.cacheId] &&
				this.cache[this.options.cacheId][this.url] &&
				this.cache[this.options.cacheId][this.url][encodeURIComponent(token)]&&
				this.cache[this.options.cacheId][this.url][encodeURIComponent(token)][this.offset]
			){		//ha már szerepel a cache-ben
				this.updateChoices(this.cache[this.options.cacheId][this.url][encodeURIComponent(token)][this.offset]);
			this.addPager();
		}
		else{		//control.js-ben definiálva prototype frissítésnél frissíteni kel, ha ott változik
			if(this.cache[this.options.cacheId]['count']++ >= this.saveCacheToServerAt){
				this.saveCacheToServer();
				this.cache[this.options.cacheId]['count'] = 0;
			}
			this.startIndicator();		
			var entry = encodeURIComponent(this.options.paramName) + '=' + encodeURIComponent(this.getToken());
			this.options.parameters = this.options.callback ? this.options.callback(this.element, entry) : entry;
			if(this.options.defaultParams)
				this.options.parameters += '&' + this.options.defaultParams;
			this.options.parameters += '&' + encodeURIComponent('data[offset]') + '=' + this.offset;	//rrd mod
			new Ajax.Request(this.url, this.options);
		}
	},
	
	onComplete: function($super, request){		//az ajax eredmények feldolgozása, megjelenítése
		var theValue = request.request.parameters[this.element.name];
		this.cache[this.options.cacheId][request.request.url] = this.cache[this.options.cacheId][request.request.url] ? this.cache[this.options.cacheId][request.request.url] : {};
		this.cache[this.options.cacheId][request.request.url][theValue] = this.cache[this.options.cacheId][request.request.url][theValue]? this.cache[this.options.cacheId][request.request.url][theValue] : {};
		this.cache[this.options.cacheId][request.request.url][theValue][this.offset] = (request.responseText || request.responseJSON);
		$super(request);
		this.addPager();
	},
	
	addPager : function(){
		if(this.offset != 0) {
			var a = new Element('a',
				{
					'class' : 'fl pici',
					href : '#',
					title : 'vissza'
				}).update('<<');
			Event.observe(a, "click", this.prev.bindAsEventListener(this));
			this.update.appendChild(a);
		}
		
		if(this.entryCount == this.pagesize){
			var a = new Element('a',
				{
					'class' : 'fr pici',
					href : '#',
					title : 'tovább'
				}).update('>>');
			Event.observe(a, "click", this.next.bindAsEventListener(this));
			this.update.appendChild(a);
		}
	},
	
	prev: function(event) {
		if(this.offset == 0) {
			return;
		}
		this.offset-= this.pagesize;
		if(this.offset < 0) {
			this.offset = 0;
		}
		Event.stop(event);
		this.element.focus();
		this.activate();
	},
	
	next: function(event) {
		this.offset+= this.pagesize;
		Event.stop(event);
		this.element.focus();
		this.activate();
	},
	
	onHideAfterBlur: function() {
		if(!this.hasFocus) {
			this.hide();
		}
	},

	onBlur: function(event) {
		setTimeout(this.onHideAfterBlur.bind(this), 250);
		this.hasFocus = false;
		this.active = false;
	},
	
	saveCache : function(){
		var expires = new Date();
		var minutes = 30;
		expires.setTime(expires.getTime() + (minutes * 60 * 1000));
		setCookie('laksmi.cache', encodeURIComponent(Object.toJSON(Ajax.CachedAutocompleter.prototype.cache)), expires, '/');
	},
	
	saveCacheToServer : function(){
		//ideiglenes metódus egy alap cahe felépítéséhez
			new Ajax.Request(laksmi.baseUrl + 'settings/savecache',
										{
											method 		:	'post',
											parameters	:	'cache=' + encodeURI(Object.toJSON(Ajax.CachedAutocompleter.prototype.cache))
										});
	}
});

Event.observe(window, 'unload', Ajax.CachedAutocompleter.prototype.saveCache);